<template>
  <div class="app-container">
    <div class="change">
      <p>
        <span>课程名称</span>
        <el-input type="text" v-model="tempData.title" class="userselect" placeholder="请输入课程名称" />
      </p>
      <p>
        <span>讲师</span>
        <el-select v-model="tempData.teacherId" placeholder="请选择讲师" class="userselect">
          <el-option v-for="item in teacherData" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <!-- <el-input type="text" v-model="tempData.level" placeholder="请输入头衔" class="userselect" /> -->
      </p>
      <p>
        <span>封面图片</span>
        <el-upload
          class="avatar-uploader"
          action
          :http-request="designUpAvatar"
          :show-file-list="false"
        >
          <img v-if="tempData.cover" :src="tempData.cover" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </p>
    </div>
    <div class="change1">
      <p>
        <span>课程简介</span>
        <Tinymce ref="editor" v-model="content" class="userselect" :height="200" />
      </p>
    </div>
    <div class="btns">
      <el-button type="primary" @click="save">保存</el-button>
    </div>
  </div>
</template>

<script>
let fatherId = null;
import Tinymce from "@/components/Tinymce";
import {
  selectMiaoshu,
  addKecheng,
  updateKecheng,
  deleteKecheng,
  allTeacherList,
  uploadFile
} from "@/api/people";
export default {
  components: {
    Tinymce,
  },
  data() {
    return {
      content: "",
      writeOradd: 0, //新增0，修改1
      tempData: {},
      teacherData:[]
    };
  },
  mounted() {
    console.log(this.$route.params);
    this.teacherList()
    // 获取课程父级分类fatherId，用于修改增加
    const temp = JSON.parse(this.$route.params.father);
    fatherId = temp.twoId;
    if (this.$route.params.options) {
      this.writeOradd = 1;
      this.tempData = JSON.parse(this.$route.params.options);
      this.content = this.tempData.description
    } else {
      this.writeOradd = 0;
    }
  },
  methods: {
    /**
     * 保存修改内容
     */
    /**
     * 获取讲师列表
     */
    teacherList() {
      allTeacherList()
        .then((res) => {
          console.log(res);
          if (res.data) {
            this.teacherData = res.data.items;
          }
        })
        .catch((res) => {
          console.log(res);
        });
    },
    // 上传头像
    designUpAvatar(item){
      console.log(this.fileNum)
      var formData = new FormData()
      const loading = this.$loading({
        lock: true,
        text: '上传中请稍等...',
        spinner: 'el-icon-loading',
        background: 'rgba(255, 255, 255, 0.8)'
      })
      formData.append("file", item.file)
      uploadFile(formData).then(res => {
        console.log(res)
        this.$message({
          showClose: true,
          message: '添加成功',
          type: "success"
        })
        loading.close()
        // this.imageUrl = res
        this.tempData.cover = res
        this.$forceUpdate()
      })
    },
    /**
     * 保存
     */
    save(){
      if(this.writeOradd !== 1){
        this.tempData.id = +new Date()
        this.tempData.subjectId = fatherId
        this.tempData.description = this.content
        addKecheng(this.tempData).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.go(-1);
        })
      }else{
        this.tempData.description = this.content
        updateKecheng(this.tempData).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.go(-1);
        })
      }
    }
  },
};
</script>

<style scoped lang="scss">
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e !important;
  border-color: #18a05e !important;
  border: none
}
.avatar-uploader .el-upload {
  border: 1px dashed #999 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatars{
	width: 45px;
	height: 45px;
  margin: 0 auto;
  border: 1px solid #ccc
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
	border: 1px dashed #CCCCCC;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
  border: 1px solid #ccc;
}
.upload-demo{
  margin: 0 10px;
}
.btns{
  background: #fff;
  display: flex;
  justify-content: flex-end;
  padding: 0 20px;
}
.change1{
  display: flex;
  flex-wrap: wrap;
  background: #fff;
  padding: 20px;
  > p {
    width: 100%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    .userselect {
      // max-width: 250px;
      flex: 1;
    }
    > span {
      display: block;
      width: 80px;
      text-align: right;
      padding-right: 15px;
    }
  }
}
.change {
  padding: 20px;
  border-radius: 10px;
  background: #fff;
  display: flex;
  flex-wrap: wrap;
  > p {
    width: 50%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
    }

    > span {
      display: block;
      width: 80px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
</style>